var form;
var table;
var layer;
layui.use(['layer','form','table'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;
    //加载优惠券信息
    selCouponUsed();
    //修改优惠券状态
    form.on('switch', function(data){
        var flag=data.elem.checked;
        //获取优惠券状态
        var online=null;
        if(flag){
            online=2;
        }else{
            online=3;
        }
        //优惠券id
        var coupon_id=data.elem.value;
        $.ajax({
            url: "/SyOperationController/updCouponOnline",
            dataType: "json",
            data:{"online":online,"coupon_id":coupon_id},
            success: function (data) {
                if(data.code==0){
                    layer.msg('修改成功');
                }else{
                    layer.msg('修改失败');
                }
            }
        });
    });

});
//刷新
function refresh() {
    location.reload();
}


//优惠券数据
function selCouponUsed(){
    var account=$("input[name='account']").val();
    var nickname=$("input[name='nickname']").val();
    $.ajax({
        url: "/SyOperationController/selCouponUsed",
        type: "post",
        dataType: "json",
        data:{"account":account,"nickname":nickname},
        success: function (data) {
            getTable(data.data);
        }
    });
}

//优惠券记录数据列表
function getTable(data){
    var tb1=table.render({
        elem:"#table_id",
        title:"优惠券记录数据列表",
        limit:5,
        data:data,
        page:true,
        cols:[[
            {type:'checkbox',width:60},
            {field:'user_id',title:'用户ID',align:'center'},
            {field:'account',title:'用户账号',align:'center'},
            {field:'nickname',title:'用户昵称',align:'center'},
            {field:'used',title:'已使用',align:'center'},
            {field:'notUsed',title:'未使用',align:'center'},
            {field:'expired',title:'已过期',align:'center'},


            {field:'',title:'操作',align:'center',templet:function(d){
                    return "<span style='margin-left: 20px'><a  onclick='javascript:CouponGiving(\""+d.user_id+"\")' style='color:#1ABC9C;'>赠送优惠券</a></span>"
                }}
        ]]
    });
}


//点击查看明细跳到明细页面
function CouponDetailsSelelct(user_id){
    location.href="CouponDetailsSelelct.html?"+user_id;
}


function getTable2(data){
    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>优惠券详情</div>",
        area : [ '670px', '450px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CouponDetails.html",
        success:function(layero,index){

            //处理开始时间
            var startDate=data.start_date;
            startDate = startDate.slice(0, startDate.indexOf("."));
            startDate = startDate.replaceAll('T',' ');

            //处理结束时间
            var endDate=data.end_date;
            endDate = endDate.slice(0, endDate.indexOf("."));
            endDate = endDate.replaceAll('T',' ');

            //处理优惠券状态
            var online=null;
            if(data.online==2){
                online="上线";
            }else if(data.online==3){
                online="不上线";
            }

            //获取商品状态
            var sate=null;
            if(data.product_sate==0){
                sate="删除";
            }else if(data.product_sate==1){
                sate="已上架";
            }else if(data.product_sate==2){
                sate="审核中"
            }

            //处理获取方式
            var mode=null;
            if(data.mode==2){
                mode="自动发放"
            }else if(data.mode==3){
                mode="活动领取"
            }else if(data.mode==4){
                mode="人工发放"
            }else if(data.mode==5){
                mode="无限制"
            }

            var obj=layero.find('iframe').contents();
            obj.find('input[name=couponCode]').val(data.coupon_code);
            obj.find('input[name=couponName]').val(data.coupon_name);
            obj.find('input[name=startDate]').val(startDate);
            obj.find('input[name=endDate]').val(endDate);
            obj.find('input[name=online]').val(online);
            obj.find('input[name=mode]').val(mode);
            obj.find('input[name=surplus]').val(data.surplus+" 张");
            obj.find('#remark').val(data.remark);

        }
    });
    window.layer_1=layer;
}


//赠送优惠券
function CouponGiving(user_id) {
    $("#user_id").val(user_id)
    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>赠送优惠券</div>",
        area : [ '870px', '650px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CouponGiving.html",
        success:function(layero,index){

        }
    });
    window.layer_1=layer;
}













